#import "dfhgwhruebvhrheyprofessionalincidentally.h"
#import "dfhgwhruebvhrheyfacultyauto.h"
#import "dfhgwhruebvhrheymisunderstandvacant.h"
@interface dfhgwhruebvhrheymisunderstandvacant ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tabIV;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (nonatomic, strong) PPP_CouponItemModel *selectedCoupon;
@end
static NSString *cellIdentifier = @"dfhgwhruebvhrheyfacultyauto";
@implementation dfhgwhruebvhrheymisunderstandvacant {
	NSArray *dataArray;
	NSIndexPath *lastIndexPath;
	PPP_CouponItemModel *lastSelect;
    NSInteger last;
    NSInteger current;
	void(^_callback)(UIButton *sender, PPP_CouponItemModel *select);
}
- (void)awakeFromNib {
	[super awakeFromNib];
	self.layer.cornerRadius = 20;
	self.layer.masksToBounds = YES;
	self.backgroundColor = [UIColor whiteColor];
	self.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
	self.tabIV.delegate = self;
	self.tabIV.dataSource = self;
	self.tabIV.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.tabIV registerNib:[UINib nibWithNibName:@"dfhgwhruebvhrheyfacultyauto" bundle:nil] forCellReuseIdentifier:cellIdentifier];
}
- (void)bindCouponList:(NSArray<PPP_CouponItemModel *> *)list andCallBack:(void(^)(UIButton *sender, PPP_CouponItemModel *select))callback {
	dataArray = list;
	_callback = callback;
	for (PPP_CouponItemModel *item in list) {
		if (item.selected) {
			lastSelect = item;
			self.selectedCoupon = item;
		}
	}
	NSUInteger row = [list indexOfObject:lastSelect];
	lastIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
	[self.tabIV reloadData];
}
- (IBAction)btnsClick:(UIButton *)sender {
    if (sender.tag == 100) {
        if (last == current) {
            self.selectedCoupon.selected = !self.selectedCoupon.selected;
        }else {
            PPP_CouponItemModel *_last = dataArray[last];
            PPP_CouponItemModel *_current = dataArray[current];
            _last.selected = !_last.selected;
            _current.selected = !_current.selected;
        }
    }
	if (_callback) {
		_callback(sender, self.selectedCoupon);
	}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    current = indexPath.row;
    last = lastIndexPath.row;
	PPP_CouponItemModel *coupon = dataArray[indexPath.row];
	if (lastSelect == coupon) {
		coupon.selected = !lastSelect.selected;
	}else {
		lastSelect.selected = NO;
		coupon.selected = YES;
		lastSelect = coupon;
	}
	[tableView reloadRowsAtIndexPaths:@[indexPath, lastIndexPath] withRowAnimation:UITableViewRowAnimationFade];
	lastIndexPath = indexPath;
	self.selectedCoupon = coupon;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	dfhgwhruebvhrheyfacultyauto *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.inView = YES;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell bindData:dataArray[indexPath.row]];
	return cell;
}
@end
